﻿using System.Windows;
using System.Windows.Controls;
using CheckLift.Linq;
using CheckLift.ViewModels;
using System;
using CheckLift.UI.Controls;
using CheckLift.Views;
using CheckLift.Common.Intf;

namespace CheckLift
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ReportViewModel reportViewModel;
        private User activeUser = null;
        public MainWindow()
        {           
            activeUser = new UserData().GetUser();

            InitializeComponent();
            reportViewModel = new ReportViewModel(activeUser.Id);
            this.Loaded += (s, e) => DataContext = reportViewModel;
            dgReportsByCompany.OnDataGridSelectedData += new OnDataGridSelectedDataEvent(OnDataGridSelectedDataEvent);
            dgReportsByService.OnDataGridSelectedData += new OnDataGridSelectedDataEvent(OnDataGridSelectedDataEvent);
        }

        private void cbCompany_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems[0] is ICompany)
            {
                if (dgReportsByCompany != null)
                    dgReportsByCompany.MainFilterParamGUID = (e.AddedItems[0] as ICompany).Id;
            }
            else
            {
                if (dgReportsByService != null)
                    dgReportsByService.MainFilterParamGUID = (e.AddedItems[0] as ServiceCompany).Id;
            }
        }

        private void OnDataGridSelectedDataEvent(object sender, DataGridSelectedArgs data)
        {
            foreach (var item in data.DataList)
            {
                if (item is IReport)
                {
                    ReportDetailView reportDetailView = new ReportDetailView();
                    //reportDetailView.Closing += new CancelEventHandler(reportDetailView_Closing);
                    reportDetailView.InitDataContext(activeUser, (IReport)item);
                    reportDetailView.Show();

                }
                
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuOptionsOpen menuOption = MenuOptionsOpen.None;
            if (Enum.TryParse((sender as MenuItem).Tag.ToString(), out menuOption))
            {
                PickUpWnd pickUp = new PickUpWnd();
                switch (menuOption)
                {
                    case MenuOptionsOpen.None:
                        return;
                    case MenuOptionsOpen.Company:
                        {
                            pickUp.Data = reportViewModel.Companies;
                            pickUp.DialogTitle = "Companies";
                            break;
                        }
                    case MenuOptionsOpen.User:
                        {
                            pickUp.Data = reportViewModel.GetAllCustomersByUser();
                            pickUp.DialogTitle = "Customers";
                            break;
                        }
                    case MenuOptionsOpen.Lift:
                        {
                            pickUp.Data = reportViewModel.Lifts;
                            pickUp.DialogTitle = "Lifts";
                            break;
                        }
                    default:
                        break;
                }
                pickUp.ShowDialog();
            }
        }

        private void Exit_Button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ReportDetailView reportDetailView = new ReportDetailView();            
            reportDetailView.InitDataContext(activeUser, null);
            reportDetailView.Show();
        }

    }

    public enum MenuOptionsOpen
    {
        None,
        Company,
        User,
        Lift,
    }
}
